package com.qianfeng.listviewduobuju;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/12.
 */
public class MyAdapter extends BaseAdapter {
   private List<IItem> mList;
   private Context mContext;

    public MyAdapter(Context context) {
        mContext = context;
        mList=new ArrayList<>();
    }

    public void setList(List<IItem> list) {
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getptype();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int positionn, View view, ViewGroup viewGroup) {
          int type=getItemViewType(positionn);
        ViewHolder0 holder0=null;
        ViewHolder1 holder1=null;
        ViewHolder2 holder2=null;
        if (view == null) {
            switch (type){
                case 0://R.layout.holder0
                     holder0=new ViewHolder0();
                     view= LayoutInflater.from(mContext).inflate(R.layout.holder0,viewGroup,false);
                     holder0.mTextView0= (TextView) view.findViewById(R.id.textview0_0);
                     view.setTag(holder0);
                    break;
                case 1:
                    holder1=new ViewHolder1();
                    view= LayoutInflater.from(mContext).inflate(R.layout.holder1,viewGroup,false);
                    holder1.mTextView0= (TextView) view.findViewById(R.id.tv1_1);
                    holder1.mTextView1= (TextView) view.findViewById(R.id.tv1_2);
                    view.setTag(holder1);
                    break;
                case 2:
                    holder2=new ViewHolder2();
                    view= LayoutInflater.from(mContext).inflate(R.layout.holder2,viewGroup,false);
                    holder2.mTextView0= (TextView) view.findViewById(R.id.tv2_1);
                    holder2.mTextView1= (TextView) view.findViewById(R.id.tv2_2);
                    holder2.mTextView2= (TextView) view.findViewById(R.id.tv2_3);
                    view.setTag(holder2);

                    break;
            }
        }else {
            switch (type){
                case 0:
                   holder0= (ViewHolder0) view.getTag();
                    break;
                case 1:
                   holder1= (ViewHolder1) view.getTag();
                    break;
                case 2:
                    holder2= (ViewHolder2) view.getTag();
                    break;
            }

        }
/////////////赋值
        switch (type){
            case 0:
                Item0 item0= (Item0) mList.get(positionn);
                holder0.mTextView0.setText(item0.getInfo());
                break;
            case 1:
                Item1 item1= (Item1) mList.get(positionn);
                holder1.mTextView0.setText(item1.getInfo1());
                holder1.mTextView1.setText(item1.getInfo2());
                break;
            case 2:
                Item2 item2= (Item2) mList.get(positionn);
                holder2.mTextView0.setText(item2.getInfo1());
                holder2.mTextView1.setText(item2.getInfo2());
                holder2.mTextView2.setText(item2.getInfo3());

                break;
        }


        return view;
    }

    static class ViewHolder0{
        TextView mTextView0;
    }
    static class ViewHolder1{
        TextView mTextView0,mTextView1;
    }
    static class ViewHolder2{
        TextView mTextView0,mTextView1,mTextView2;
    }

}
